
const {merge} = require('webpack-merge');
const prodBase = require('./webpack.prod.base');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// eslint-disable-next-line no-unused-vars
const TerserWebpackPlugin = require('terser-webpack-plugin');
const {appHtml, resolveApp} = require('./paths');
// css压缩
// eslint-disable-next-line no-unused-vars
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

/* eslint-disable no-unused-vars */
const { defaultSplitChunks, vueCliSplitChunks, optimizeSplitChunks } = require('./splitChunksRules');
const webpack = require('webpack');
let config = merge(prodBase, {
    optimization: {
        // emitOnErrors: true, // 默认开启，出现错误时不输出文件
        // concatenateModules: true, // 默认开启，作用域提升
        //开启scope-hoisting
        // usedExports: true, // 启用标记功能。生产模式下，默认是启用的,可用于tree-shaking
        // concatenateModules: true, // 生成模式下，默认是启用的
        minimize: true, // 开启压缩
        minimizer: [  // 自定义TerserWebpackPlugin实例配置
            // 压缩css
            new CssMinimizerPlugin({ 
                parallel: true, // 启用/禁用多进程并发执行
            }), 
            // js压缩
            new TerserWebpackPlugin({
                parallel: true,// 开启多进程
                test: /\.js(\?.*)?$/i, // 压缩js文件
                extractComments: false, // 不抽取注释
                terserOptions: {
                    // 如何移除项目中无用的 console.log 代码: https://mp.weixin.qq.com/s/XI8h70GTD8f7CaGK4NkcnQ
                    compress: {
                        unused: true, // 删除无用代码
                        warnings: true,
                        drop_console: true,
                        drop_debugger: true,
                        dead_code: true,
                        pure_funcs: ['console.log', 'console.table'] // 删除console
                    }
                }
            })
        ],
        splitChunks: optimizeSplitChunks
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'webpack5脚手架',
            template: appHtml,
            favicon: resolveApp('public/favicon.ico'),
        }),
        // new webpack.NoEmitOnErrorsPlugin(),// 默认开启，出现错误时不输出文件
        // new webpack.optimize.ModuleConcatenationPlugin(),// 默认开启，作用域提升

        // 该插件将根据模块的相对路径生成哈希值，生成一个四字符的字符串作为模块 id。建议在生产中使用
        // new webpack.ids.HashedModuleIdsPlugin({}),
    ]
});
module.exports = config;